﻿using System;
using System.Linq;

namespace BoxSauce.AgencyToolkit.Strings
{
    public static class StringHelpers
    {
        private const string AlphaCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        /// <summary>
        /// Retrieves a substring from this instance. The substring
        /// contains the amount of words specified in wordCount
        /// </summary>
        /// <param name="stringToChange">String to return substring from</param>
        /// <param name="wordCount">Number of words to return</param>
        /// <param name="elipsis">If true adds elipsis character to end of string</param>
        /// <returns>Substring of instance containing wordCount amount of words.</returns>
        public static string SubstringWords(string stringToChange, int wordCount, bool elipsis = false)
        {
            var retVal = (from w in stringToChange.Split(' ')
                          select w).Take(wordCount).ToArray();

            if (elipsis)
                return string.Concat(string.Join(" ", retVal), "…");

            return string.Join(" ", retVal);
        }

        /// <summary>
        /// Returns the given string truncated to the specified length, suffixed with an elipses (...)
        /// </summary>
        /// <param name="input"><see cref="System.String" /></param>
        /// <param name="length">Maximum length of return string</param>
        /// <returns></returns>
        public static string Truncate(string input, int length)
        {
            return Truncate(input, length, "...");
        }

        /// <summary>
        /// Returns the given string truncated to the specified length, suffixed with the given value
        /// </summary>
        /// <param name="input"><see cref="System.String" /> to truncate</param>
        /// <param name="length">Maximum length of return string</param>
        /// <param name="suffix">The value to suffix the return value with (if truncation is performed)</param>
        /// <returns></returns>
        public static string Truncate(string input, int length, string suffix)
        {
            if (input == null) return "";
            if (input.Length <= length) return input;

            if (suffix == null) suffix = "...";

            return input.Substring(0, length - suffix.Length) + suffix;
        }

        /// <summary>
        /// Checks to see if <see cref="System.String"/> can to parsed 
        /// to an <see cref="System.int" />
        /// </summary>
        /// <param name="text"><see cref="System.String"/> to parse</param>
        /// <returns>True if is <see cref="System.int"/></returns>
        public static bool IsInt(this string text)
        {
            int integer;
            return Int32.TryParse(text, out integer);
        }

        /// <summary>
        /// Removes all characters from a string that are not in the 'keepTheseCharacters' parameter
        /// </summary>
        /// <param name="str"></param>
        /// <param name="keepTheseCharacters"></param>
        /// <returns></returns>
        public static string CleanString(this string str, string keepTheseCharacters = AlphaCharacters)
        {
            return new string((from c in str.ToCharArray()
                               where keepTheseCharacters.Contains(c)
                               select c).ToArray());
        }
    }
}
